What is a-sync-waterfall?
The a-sync-waterfall npm package is used to run an array of asynchronous functions in series, each passing their results to the next in the array. However, if any of the functions pass an error to the callback, the next function is not executed, and the main callback is immediately called with the error.
What are a-sync-waterfall's main functionalities?
Running asynchronous tasks in series
This feature allows you to run multiple asynchronous tasks in series, where each task waits for the previous one to complete before starting. The results of each task are passed to the next task in the array.
const waterfall = require('a-sync-waterfall');
waterfall([
function(callback) {
setTimeout(() => {
console.log('Task 1');
callback(null, 'one');
}, 1000);
},
function(arg1, callback) {
setTimeout(() => {
console.log('Task 2');
callback(null, 'two');
}, 500);
},
function(arg1, callback) {
setTimeout(() => {
console.log('Task 3');
callback(null, 'three');
}, 100);
}
], function (err, result) {
if (err) {
console.error('Error:', err);
} else {
console.log('All tasks completed. Final result:', result);
}
});
Error handling in series of tasks
This feature demonstrates how errors are handled in a series of tasks. If any task passes an error to the callback, the subsequent tasks are not executed, and the main callback is immediately called with the error.
const waterfall = require('a-sync-waterfall');
waterfall([
function(callback) {
setTimeout(() => {
console.log('Task 1');
callback(null, 'one');
}, 1000);
},
function(arg1, callback) {
setTimeout(() => {
console.log('Task 2');
callback('Error in Task 2');
}, 500);
},
function(arg1, callback) {
setTimeout(() => {
console.log('Task 3');
callback(null, 'three');
}, 100);
}
], function (err, result) {
if (err) {
console.error('Error:', err);
} else {
console.log('All tasks completed. Final result:', result);
}
});
Other packages similar to a-sync-waterfall
async
The async package provides a collection of functions for working with asynchronous JavaScript. It includes methods for running tasks in series, parallel, and other control flow patterns. Compared to a-sync-waterfall, async offers a broader range of utilities and more flexibility in handling asynchronous operations.
async-waterfall
The async-waterfall package is another implementation of the waterfall pattern for running asynchronous tasks in series. It is similar to a-sync-waterfall but is part of the larger async library, which provides additional utilities for managing asynchronous code.
neo-async
Neo-async is a high-performance drop-in replacement for async. It provides similar functionality with a focus on speed and efficiency. Like async, it includes methods for running tasks in series, parallel, and other control flow patterns, making it a versatile alternative to a-sync-waterfall.
a-sync-waterfall
Simple, isolated sync/async waterfall module for JavaScript.
Runs an array of functions in series, each passing their results to the next in
the array. However, if any of the functions pass an error to the callback, the
next function is not executed and the main callback is immediately called with
the error.
For browsers and node.js.
Installation
- Just include a-sync-waterfall before your scripts.
npm install a-sync-waterfall
if you’re using node.js.
Usage
waterfall(tasks, optionalCallback, forceAsync);
- tasks - An array of functions to run, each function is passed a
callback(err, result1, result2, ...)
it must call on completion. The first
argument is an error (which can be null) and any further arguments will be
passed as arguments in order to the next task. - optionalCallback - An optional callback to run once all the functions have
completed. This will be passed the results of the last task's callback.
- forceAsync An optional flag that force tasks run asynchronously even if they are sync.
Node.js:
var waterfall = require('a-sync-waterfall');
waterfall(tasks, callback);
Browser:
var waterfall = require('a-sync-waterfall');
waterfall(tasks, callback);
window.waterfall(tasks, callback);
Tasks as Array of Functions
waterfall([
function(callback){
callback(null, 'one', 'two');
},
function(arg1, arg2, callback){
callback(null, 'three');
},
function(arg1, callback){
callback(null, 'done');
}
], function (err, result) {
});
Derive Tasks from an Array.map
waterfall(myArray.map(function (arrayItem) {
return function (nextCallback) {
doAsyncThingsWith(arrayItem, nextCallback);
}}));
waterfall([function initializer (firstMapFunction) {
firstMapFunction(null, initialValue);
}].concat(myArray.map(function (arrayItem) {
return function (lastItemResult, nextCallback) {
var itemResult = doThingsWith(arrayItem, lastItemResult);
nextCallback(null, itemResult);
}})), function (err, finalResult) {
});
Acknowledgements
Hat tip to Caolan McMahon and
Paul Miller, whose prior contributions this is
based upon.
Also Elan Shanker from which this rep is forked
License
MIT